package com.yzz.hzraisedust.hzraseidust.main;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.PopupWindow;
import android.widget.TextView;
import com.yzz.hzraisedust.hzraseidust.R;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhengzhen on 2017/8/26.
 */

public class FilterDropDownList implements AdapterView.OnItemClickListener{
    private PopupWindow mPop;
    private Context mContext;
    private View mParentView;
    private CallBack mCallBack;

    private List<String> mSourceItem;
    private List<String> mShowItem;

    //需要显示的View
    private View mShowPopView;
    //ListView
    private GridView mGridView;
    //空气质量grid
    private GridView mAirQualityGridView;
    interface CallBack{
        void filterArea(String areaName,String airQualityContidion);
    }

    public  FilterDropDownList(Context context,View parent,CallBack callBack){
        mContext = context;
        mParentView = parent;
        mCallBack = callBack;
        mSourceItem = new ArrayList<>();
        mShowItem = new ArrayList<>();


    }
    private MyAdapter mSquarAdapter;

    private MyAdapter mAirSquarAdapter;
    private void initView(){
        mShowPopView = LayoutInflater.from(mContext).inflate(R.layout.map_arear_filter_layout,null,false);
        mGridView = mShowPopView.findViewById(R.id.filter_list_drop_down_gridview);
        mGridView.setOnItemClickListener(this);
        mSquarAdapter= new MyAdapter(mSourceItem);
        mGridView.setAdapter(mSquarAdapter);
        ((TextView)mParentView).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(!mPop.isShowing())
                    showPop();
                else mPop.dismiss();
            }
        });
        //空气质量
        mAirQualityGridView = mShowPopView.findViewById(R.id.filter_list_drop_down_airquality_gridview);
        mAirSquarAdapter = new MyAdapter(mShowItem);
        mAirQualityGridView.setAdapter(mAirSquarAdapter);
        mAirQualityGridView.setOnItemClickListener(new AirSelect());

       Button selectButton = mShowPopView.findViewById(R.id.select_area_air_quality_button);
       selectButton.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
               mPop.dismiss();
               String areaName = mSquarAdapter.getSelectItem();
               String airCondition = mAirSquarAdapter.getSelectItem();
               mCallBack.filterArea(areaName, airCondition);
           }
       });
    }

    public  void setItems(List<String> sourceItems,List<String> showItems){
        if(sourceItems!=null){
            for(String name : sourceItems){
                mSourceItem.add(name);
            }
        }
        if(showItems!=null){
            for(String name :showItems){
                mShowItem.add(name);
            }
        }
        initView();
        initPop();
    }

    public void showPop() {

        if(!mPop.isShowing()&&mSourceItem.size()>0)
        {
            mPop.showAsDropDown(mParentView, (int) -mParentView.getX()+mParentView.getWidth(), 5);

        }

    }
    public void initPop() {
        WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        int width = wm.getDefaultDisplay().getWidth();
        width = width-mParentView.getWidth();
        int height = ViewGroup.LayoutParams.WRAP_CONTENT;
        mPop = new PopupWindow(mShowPopView, width, height, true);

      //  mPop.setOnDismissListener(this);// pop消失的监听事件
        // 设置弹出框背景
        mPop.setBackgroundDrawable(new ColorDrawable(0xffffffff));

    }


    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        mSquarAdapter.setSelectIndex(i);
        mSquarAdapter.notifyDataSetChanged();
    }

    class MyAdapter extends BaseAdapter{
        private List<String> sourceItems;
        public MyAdapter(List<String> items){
            sourceItems = items;
        }

        private int mSelectItem = 0;

        public void setSelectIndex(int selectItem) {
            this.mSelectItem = selectItem;
        }
        public String getSelectItem(){
            return sourceItems.get(mSelectItem);
        }

        @Override
        public int getCount() {
            return sourceItems.size();
        }

        @Override
        public Object getItem(int i) {
            return sourceItems.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
                ViewHolder viewHolder = null;
                if(view == null){
                    view = LayoutInflater.from(mContext).inflate(R.layout.filter_drop_down_list_item_layout,null,false);

                    viewHolder = new ViewHolder();
                    viewHolder.areaNameTextView = view.findViewById(R.id.filter_area_name_textview);
                   // viewHolder.areaFilterGridView = view.findViewById(R.id.filter_item_gridview);
                    view.setTag(viewHolder);
                }
                if(viewHolder == null){
                    viewHolder = (ViewHolder)view.getTag();
                }
            if(mSelectItem == i){
                viewHolder.areaNameTextView.setBackgroundColor(mContext.getResources().getColor(R.color.button_click));
                viewHolder.areaNameTextView.setTextColor(Color.WHITE);
                }else{
                viewHolder.areaNameTextView.setBackgroundColor(Color.TRANSPARENT);
                viewHolder.areaNameTextView.setTextColor(Color.BLACK);
            }
            viewHolder.areaNameTextView.setText(sourceItems.get(i));
            return view;
        }

        class ViewHolder{
             TextView areaNameTextView;
        }

    }

    class AirSelect implements AdapterView.OnItemClickListener{

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            mAirSquarAdapter.setSelectIndex(i);
            mAirSquarAdapter.notifyDataSetChanged();
        }
    }
}
